feat(transaction): add useTransactionSimulation hook#2639
Open
lau90eth wants to merge 1 commit into
Open
Conversation
Adds transaction simulation to prevent failed transactions before signing. - useTransactionSimulation hook: simulates transactions, detects failures - simulateTransaction utility: uses viem eth_call, zero external dependencies - Shows: success ✅, failure 🔴 with error message - Detects: revert, insufficient funds, gas exceeded - 5 tests covering: disabled, empty state, success, failure, API error Refs: coinbase#2572
|
@lau90eth is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
🟡 Heimdall Review Status
|
Author
|
Hi @coinbase/onchainkit-maintainers, Gentle ping on #2639 ( This hook allows simulating transactions before the user signs them — one of the most requested security features for OnchainKit. I've also published a companion security suite here for easier testing: Happy to address any feedback or adjust the implementation. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every user on Base has paid gas for a transaction that failed.
"Insufficient liquidity", "execution reverted", "out of gas" —
all discovered after signing and paying.
OnchainKit has zero pre-flight checks before the user confirms.
This PR adds
useTransactionSimulation, a hook that simulatestransactions before signing — showing exactly what will happen,
or warning if it will fail.
What changed
useTransactionSimulationhook — simulates all calls fromTransactionContextbefore the user signs. Exposes success/failurestatus, decoded error messages, gas estimate, and warnings.
simulateTransactionutility — uses viemeth_callto dry-runa transaction against the current chain state. Zero external API
dependencies — reads directly from chain via the configured RPC.
SimulationResulttype — standardized shape including successflag, gasUsed, returnData, decoded error, and warnings array.
src/transaction/index.tsUsage
API
Notes to reviewers
eth_calldirectly.No Tenderly, no Alchemy simulation API, no API key required.
Works on any EVM chain configured in
OnchainKitProvider.willFail— convenience flag derived fromsimulation?.success === false. Safe to use as a gate forTransactionButton disabledprop.warnings— human-readable error messages decoded fromthe revert reason. Suitable for direct UI rendering.
enabled: false— opt-out for transactions where simulationis not meaningful (e.g. pure ETH transfers with no calldata).
callsfromuseTransactionContextautomatically.callschange.APIErrorpattern as existing utilities.Testing
useTransactionSimulation.test.ts— 5 testssimulateTransaction.test.ts— utility unit testsTest cases covered:
enabled: false→ returns null immediately, no fetchsuccess: true,gasUsedpopulatedwillFail: true,warningspopulatederrorstate exposed, graceful fallbackRisk
Low. Read-only hook.
eth_calldoes not broadcast a transactionor modify any state — it is a pure simulation against current
chain state.
Returns
nullwhenenabled: falseor no calls present.Does not modify
TransactionProvider,TransactionButton,or any existing transaction logic.